package com.stylefeng.guns.modular.system.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.constant.factory.PageFactory;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.persistence.dao.AreaMapper;
import com.stylefeng.guns.common.persistence.model.Dict;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.service.impl.AreaServiceImpl;
import com.stylefeng.guns.modular.system.warpper.AreaWarpper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.common.persistence.model.Area;
import com.stylefeng.guns.modular.system.service.IAreaService;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * area控制器
 *
 * @author fengshuonan
 * @Date 2018-01-20 20:30:36
 */
@Controller
@RequestMapping("/area")
public class AreaController extends BaseController {

    private String PREFIX = "/system/area/";

    @Autowired
    private AreaServiceImpl areaService;


    /**
     * 跳转到area首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "area.html";
    }

    /**
     * 跳转到添加area
     */
    @RequestMapping("/area_add")
    public String areaAdd() {
        return PREFIX + "area_add.html";
    }

    /**
     * 跳转到修改area
     */
    @RequestMapping("/area_update/{areaId}")
    public String areaUpdate(@PathVariable Integer areaId, Model model) {
        Area area = areaService.selectById(areaId);
        model.addAttribute("item", area);
        LogObjectHolder.me().set(area);
        return PREFIX + "area_edit.html";
    }


    /**
     * 获取area列表(做分页)
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(value = "name",required = false) String name, @RequestParam(value = "pname",required = false) String pname) {
        Page<Area> page = new PageFactory<Area>().defaultPage();

        Area area = null;
        String pcode = "";
        if(ToolUtil.isNotEmpty(pname)){
            area = areaService.selectName(pname);
            pcode = area.getCode();
        }else {
            area = new Area();
        }

        //查询市
        List<Map<String, Object>> areas = areaService.selectSQ(page,pcode,name);
        page.setRecords((List<Area>) new AreaWarpper(areas).warp());
        return super.packForBT(page);
    }

    /**
     * 添加站内信父类树
     *
     * @return
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public Object tree() {
        return areaService.tree();
    }

    /**
     * 添加区域父类树
     *
     * @return
     */
    @RequestMapping(value = "/areatree")
    @ResponseBody
    public Object areatree() {
        return areaService.areatree();
    }


    /**
     * 新增area
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Area area, @Param("pcode") String pcodename) {
        if (ToolUtil.isEmpty(area)) {
            throw new GunsException(BizExceptionEnum.AREA_ADDNULLPOINTE);
        }
        String pcode = "";
        //查父类地区代码
        if (pcodename == "" || pcodename == null) {
            pcode = "0";
        } else {
            List<Area> list = areaService.selectPcode(pcodename);
            for (Area area1 : list) {
                pcode = area1.getCode();
            }
        }
        area.setPcode(pcode);
        area.setStatus(1);
        area.setCreateTime(new Date());
        area.setUpdateTime(new Date());
        areaService.insert(area);
        areaService.refreshAreaCache();
        return super.SUCCESS_TIP;
    }

    /**
     * 删除area
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer areaId) {
        areaService.deleteById(areaId);
        areaService.refreshAreaCache();
        return SUCCESS_TIP;
    }

    /**
     * 修改area
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Area area) {
        if (ToolUtil.isEmpty(area)) {
            throw new GunsException(BizExceptionEnum.AREA_UPDATENULLPOINTE);
        }
        area.setUpdateTime(new Date());
        areaService.updateById(area);
        areaService.refreshAreaCache();
        return super.SUCCESS_TIP;
    }

    /**
     * 修改地区状态为启用
     */
    @RequestMapping(value = "/statusupdate")
    @ResponseBody
    public Object statusupdate(@RequestParam Integer areaId) {
        //查询
        Area area = areaService.selectById(areaId);
        //修改
        if (area.getStatus() != 1) {
            area.setStatus(1);
            areaService.updateById(area);
            areaService.refreshAreaCache();
            return SUCCESS_TIP;
        }
        return SUCCESS_TIP;
    }

    /**
     * 修改地区状态为禁用
     */
    @RequestMapping(value = "/statusupdateno")
    @ResponseBody
    public Object statusupdateno(@RequestParam Integer areaId) {
        //查询
        Area area = areaService.selectById(areaId);
        //修改
        if (area.getStatus() != 0) {
            area.setStatus(0);
            areaService.updateById(area);
            areaService.refreshAreaCache();
            return SUCCESS_TIP;
        }
        return SUCCESS_TIP;

    }


    /**
     * area详情
     */
    @RequestMapping(value = "/detail/{areaId}")
    @ResponseBody
    public Object detail(@PathVariable("areaId") Integer areaId) {
        return areaService.selectById(areaId);
    }
}
